$(function() {
	$('a.t_info').toggle(
		function() {
			var tr = $(this).parents('tr');
			tr.css('background', '#fff');
			tr.next('tr.tdMore').show();
		},
		function() {
			var tr = $(this).parents('tr');
			tr.css('background', 'none');
			tr.next('tr.tdMore').hide();
		}
	);

	var spoNames = [''];
	if (document.location.href.search(/\/a_\w+\/generation\//) > 0) {
		$.ajax({
			async   : true,
			type    : "POST",
			url     : "/a_admin/generation/package/manual.names",
			data    : {spo: $('#spo').val()},
			dataType: "json",
			success : function(data){
				spoNames = data;
				spoNames.unshift('');
			},
			error : function() {
				//alert('Error getting SPO names');
			}
		});
	}

	var $spoSelector = $('#spo_select');
	var $spoCode = $('#spo');
	var $spoName = $('#spo_name');
	var spoRegexp = /[^a-z0-9_-]/gi;

    $spoSelector.change(function() {
        $spoCode.val(($(this).val()).replace(spoRegexp, ''));

        var spoName = spoNames[this.selectedIndex];
		if (spoName != 'undefined') {
			$spoName.val(spoName);
		}

		$spoSelector.val($spoCode.val());
		$spoSelector.blur();
    });

    $spoCode.keyup(function() {
    	var $value = this.value;
    	$value = $value.replace(spoRegexp, '');
    	this.value = $value;

    	var opts = $spoSelector.find('option[value=' + $value + ']');
    	if (opts.length > 0) {
    		$spoSelector.val($value);
    		var spoName = spoNames[$spoSelector.get(0).selectedIndex];
    		if (spoName != 'undefined') {
    			$spoName.val(spoName);
    		}
    	} else {
    		$spoSelector.val('');
    	}
    });

	$('#city_id').change(function() {
        $('#city_arrival').val(this.value);
    });

	$('ol.hotels-list li a.link-delete-hotel').click(function() {
		var $this = $(this);
		if (confirm('SURE_TO_DELETE_HOTEL'.translate())) {
			$.ajax({
				async   : true,
				type    : "POST",
				url     : "/a_admin/generation/package/manual.delete",
				data    : {index: $this.metadata().index},
				dataType: "json",
				success : function(data){
					if (data.status == 'error') {
						alert('ERROR_ORDER_RESPONSE_TEXT'.translate());
					} else if (data.status == 'success') {
						$this.parents('li').remove();
						if ($('ol.hotels-list li').length <= 0) {
							$('ol.hotels-list').prev('h3').remove();
							$('ol.hotels-list').remove();
						}
					}
				},
				error : function() {
					alert('Error with requesting ajax data.');
				}
			});
		}
	});

	function setSelectedRow(elm) {
		if (!$(elm).parents('.row').hasClass('selected')) {
			$(elm).parents('.tour_choose').find('.row').removeClass('selected');
			$(elm).parents('.row').addClass('selected');
		}
	}

	$('.s_opened .tour_choose .row .desc h4').click(function() {
		var inpRadio = $(this).parents('.row-3').find('.input input');
		inpRadio.trigger('click');
		$(this).parents('.tour_choose').find('a.tm-radiobutton img').removeClass('checked');
		inpRadio.next('a.tm-radiobutton').find('img').addClass('checked');
		setSelectedRow(this);
	});

	$('.s_opened .tour_choose .row .input a.tm-radiobutton').click(function() {
		$(this).parents('.tour_choose').find('a.tm-radiobutton img').removeClass('checked');
		$(this).find('img').addClass('checked');
		setSelectedRow(this);
	});

	$('.s_opened .tour_choose .row .desc .more-info .more-link').toggle(
		function() {
			$(this).next('.more-block').show();
		},
		function() {
			$(this).next('.more-block').hide();
		}
	);

	$('.search_results_item .item_desc h3 a').click(function() {
		var block = $(this).parents('.search_results_item').next('form.propositions');
		if (block.is(':visible'))
			block.hide();
		else
			block.show();
	});

	$('a.show-spo-desc').click(function() {
		$(this).parents('.row').find('.desc-short').hide();
		$(this).parents('.row').find('.desc-all').show();
		$(this).parents('.row').find('.spo-gallery').show();
	});
	$('a.hide-spo-desc').click(function() {
		$(this).parents('.row').find('.desc-all').hide();
		$(this).parents('.row').find('.spo-gallery').hide();
		$(this).parents('.row').find('.desc-short').show();
	});

});